package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.dto.AppRankingDto;
import com.vf.admin.client.service.*;
import com.vf.admin.common.entity.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin
@Controller
@RequestMapping("/app/ranking")
public class AppRanking extends ClientBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppIntimacyGuardService appIntimacyGuardService;
    @Autowired
    private AppFamilyService appFamilyService;
    @Autowired
    private AppMingXiShouLiService appMingXiShouLiService;
    @Autowired
    private AppMingXiSongLiService appMingXiSongLiService;
    @Autowired
    private AppMingxiMeilizhiService appMingxiMeilizhiService;
    @Autowired
    private AppUserSettingService userSettingService;

    /**
     * 排行榜
     *
     * @param type       1恩爱榜，2女神榜，3富豪榜，4男神榜，5家族榜，6魅力榜，7天籁榜
     * @param secondType 1日榜，2周榜，3月榜
     * @return
     */
    @ResponseBody
    @RequestMapping("/getRanking")
    public Result getRanking(Integer type, Integer secondType) {
        try {
            List<Map> userIdsByHiddenRank = userSettingService.getUserIdsByHiddenRank();
            String hiddenUserIds = userIdsByHiddenRank.stream().map(e -> e.get("userId").toString()).collect(Collectors.joining(","));
            Map<String, Object> params = new HashMap<>();
            if(StringUtils.isNotBlank(hiddenUserIds)) {
                params.put("hiddenUserIds", hiddenUserIds);
            }

            switch (type) {
                case 1:
                    params.remove("hiddenUserIds");
                    if(StringUtils.isNotBlank(hiddenUserIds)) {
                        for(String str : hiddenUserIds.split(",")) {
                            params.put(str, str);
                        }
                    }
                    List<AppRankingDto> rankingIntimacy = appIntimacyGuardService.getRanking(secondType, params);
                    return this.sucessJSON(rankingIntimacy, "成功");
                case 2:
                    params.put("sex", 2);
                    List<AppRankingDto> rankingNvShen = appMingXiShouLiService.getRanking(secondType, params);
                    return this.sucessJSON(rankingNvShen, "成功");
                case 3:
                    List<AppRankingDto> rankingSongLi = appMingXiSongLiService.getRanking(secondType, params);
                    return this.sucessJSON(rankingSongLi, "成功");
                case 4:
                    params.put("sex", 1);
                    List<AppRankingDto> rankingNanShen = appMingXiShouLiService.getRanking(secondType, params);
                    return this.sucessJSON(rankingNanShen, "成功");
                case 5:
                    List<AppRankingDto> rankingFamily = appFamilyService.getRanking(secondType);
                    return this.sucessJSON(rankingFamily, "成功");
                case 6:
                    List<AppRankingDto> rankingMeiLi = appMingxiMeilizhiService.getRanking(secondType, params);
                    return this.sucessJSON(rankingMeiLi, "成功");
                case 7:
                    return null;
                default:
                    return this.errorJSON("榜单类型错误");
            }
        } catch (Exception e) {
            logger.error("获取排行榜", e);
            return this.errorJSON("系统繁忙");
        }
    }


}
